package gui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Motor.Modelo;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;


@SuppressWarnings("serial")
public class VentanaMain extends JFrame {
	private Modelo _mod;
	public VentanaMain(final Modelo mod) {
		
		_mod=mod;
		
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Gastoneitor (GST)");
		setName("ventanaPrincipal");		
		setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/UTILIDADES/icon.png")));
		setSize(new Dimension(377, 319));
		setResizable(false);
		
		JTabbedPane panelMenu = new JTabbedPane();
		getContentPane().add(panelMenu, BorderLayout.CENTER);
		
		JPanel mesActual = new MesActual(mod);
		//mesActual.setName("");
		panelMenu.addTab("Mes Actual", null, mesActual, null);
		
		JPanel cuentas = new CuentasPanel(mod);
		panelMenu.addTab("Cuentas", null, cuentas, null);
		
		JPanel pasados = new PasadosPanel(mod);
		panelMenu.addTab("Pasados", null, pasados, null);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		panelMenu.addChangeListener(new ChangeListener() {			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				MesActual.updateTabla(mod);
				PasadosPanel.updateTabla(mod);
				CuentasPanel.updateTabla(mod);
			}
		});
		
		
	}
	 protected void processWindowEvent(WindowEvent e) {

	        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
	        
	            int exit = JOptionPane.showConfirmDialog(null,"SheGuRoH Ke tE VaH a KeRe PiRaH ShUpRiMo?? \n(Los datos seran guardados)"
	            +"\nEl CoDiGoH CoMpIlA y tU Te KaLlA y lO AsImIlaH", "Te larga' ? O ke ase ?",JOptionPane.YES_NO_OPTION);
	            //conste que me aburro mucho
	            if (exit == JOptionPane.YES_NO_OPTION) {
	            	_mod.Save();
	                System.exit(0);
	            }
	            
	        } else 
	           super.processWindowEvent(e);
	        
	    }
	
}
